package com.dlc.shop.jky.common.foreign.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.dlc.shop.bean.dto.WarehouseDTO;
import com.dlc.shop.common.foreign.ipPortMap;
import com.dlc.shop.config.ShopConfig;
import com.dlc.shop.jky.common.foreign.service.ForeignWarehouseService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@AllArgsConstructor
public class ForeignWarehouseServiceImpl implements ForeignWarehouseService {

    private final ShopConfig shopConfig;

    public String getUrlIpPort(){
        ipPortMap ipPortMap = shopConfig.getForeignUrl();
        return ipPortMap.getUrl();
    }

    @Override
    public String save(WarehouseDTO warehouseDTO) {
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/warehouse/save";
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(warehouseDTO)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }

    @Override
    public String delete(Long warehouseId) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/warehouse/delete/"+warehouseId;
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json"); // 设置请求头
                //.body(JSON.toJSONString(brand)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }

    @Override
    public String update(WarehouseDTO warehouseDTO) {
        String urlIpPort = getUrlIpPort();
       if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/warehouse/update";
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(warehouseDTO)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+url+" 返回数据---》"+response.body());
        return response.body();
    }
}
